package com.duoketech.controller.weixin;

import com.duoketech.config.Constant;
import com.duoketech.controller.BaseController;
import com.duoketech.exception.ValidateException;
import com.duoketech.model.room.UnitRoom;
import com.duoketech.model.roomorder.RoomOrder;
import com.duoketech.service.web.room.UnitRoomService;
import com.duoketech.service.web.unit.UnitService;
import com.duoketech.service.weixin.activity.WeiXinActivityService;
import com.duoketech.service.weixin.room.WeiXinRoomService;
import com.duoketech.utils.PageUtil;
import com.duoketech.utils.pay.weixin.JSSDKInterceptor;
import com.jfinal.aop.Before;
import com.jfinal.kit.Kv;
import com.jfinal.plugin.activerecord.Page;

public class WxRoomController extends BaseController {


    public void index(){
        render("index.html");
    }

    public void ajaxRoom(){
        int pageNum = getParaToInt("pageSize",1);
        Page<UnitRoom> unitRoom = WeiXinRoomService.ME.queryUnitRoom(pageNum,10);
        Kv kv = Kv.by("unitRoom",unitRoom);
        renderObject(kv);
    }

    public void queryRoomById(){
        int id = getParaToInt("id",0);
        UnitRoom unitRoom = WeiXinRoomService.ME.queryRoomById(id);
        unitRoom.put("unit",unitRoom.unitId());
        renderObject(unitRoom);
    }

    public void ajaxDoRoomOrder(){
        try {
            boolean result = validateToken(Constant.TOKEN_KEY);
            if(!result){
                renderError("请勿重复提交当前客栈预订信息");
                return;
            }
            RoomOrder roomOrder = getModel(RoomOrder.class);
            String totalPrice = getPara("total_price","1.0");
            roomOrder.set(RoomOrder.MEMBER_ID,getMemberId());
            roomOrder.set(RoomOrder.TOTAL_PRICE,PageUtil.yuanToFeng(Double.valueOf(totalPrice)));
            int id = UnitRoomService.ME.doAddRoomOrder(roomOrder);
            renderObject(id);
        } catch (ValidateException e) {
            renderError(e.getMessage());
        }
    }

    public void roomDetail(){
        int id = getParaToInt("id",0);
        setAttr("id",id);
        setAttr("unitRoom",WeiXinRoomService.ME.queryRoomById(id));
        render("room-detail.html");
    }


    public void roomOrder(){
        int id = getParaToInt("id",0);
        UnitRoom room = WeiXinRoomService.ME.queryRoomById(id);
        createToken(Constant.TOKEN_KEY);
        setAttr("room",room);
        render("room-order.html");
    }

    public void cancelRoomOrder(){
        int id = getParaToInt("id",0);
        try {
            UnitRoomService.ME.cancelRoomOrder(id,getMemberId());
            renderSuccess("取消成功");
        } catch (ValidateException e) {
            renderError(e.getMessage());
        }
    }

    @Before(JSSDKInterceptor.class)
    public void roomOrderPreview(){
        int id = getParaToInt("id",0);
        try {
            RoomOrder order= WeiXinRoomService.ME.roomOrderPreview(id,getMemberId());
            setAttr("roomOrder",order);
            render("room-order-preview.html");
        } catch (ValidateException e) {
            renderError(e.getMessage());
        }
    }
}
